package com.demo.datastructure.queue;

/**
 * @description: 数组循环队列
 * @author: ljr
 * @time: 2021/4/29 9:31
 */
public class ArrayCircularQueue<E> implements MyQueue<E> {

    private final Object[] args;

    private final int capacity;

    private int head;

    private int tail;

    public ArrayCircularQueue(int capacity) {
        this.capacity = capacity;
        args = new Object[capacity];
    }

    @Override
    public boolean enqueue(E value) {
        if((tail+1)%capacity == head) return false;
        args[tail] = value;
        tail = (tail + 1)%capacity;
        return true;
    }

    @Override
    public E dequeue() {
        if(head == tail) return  null;
        E value = (E) args[head];
        args[head] = null;
        head = (head + 1)%capacity;
        return value;
    }
}
